<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/8/29
 * Time: 21:06
 */

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Auth as AuthModel;
use app\common\model\User as UserModel;
use app\common\model\Group as GroupModel;
use think\facade\Request;
use think\facade\Config;


class User extends AdminBase
{
    public function index()
    {
        if(Request::isAjax()){
            $page = Request::param('page/d');
            $limit = Request::param('limit/d');
            $list = UserModel::instance()->page($page,$limit)->select();
            $count = UserModel::instance()->count();
            $res = array('list'=>$list,'count'=>$count);
            $this->success('数据列表','',$res);
        }else{
            return $this->fetch();
        }
    }

    public function add(){
        if(Request::isPost())
        {
            $form = Request::param('form/a');
            if(UserModel::instance()->where(['username'=>$form['username']])->findOrEmpty()){
                $this->error('该用户已存在');
            }
            $form['salt'] = getSalt(6);
            $form['password'] = md5($form['password'].$form['salt']);
            $form['auths'] = '';
            UserModel::instance()->insert($form);
            $this->success('成功','');
        }else{
            $this->assign('model',array());
            return $this->fetch('user');
        }
    }

    public function edit(){
        $id = Request::param('id/d');
        $model = UserModel::instance()->where(['id'=>$id])->findOrEmpty();
        if(!$model){
            $this->error('参数错误');
        }
        if(Request::isPost()){
            $form = Request::param('form/a');
            if($model['username'] != $form['username']){
                $this->error('用户名不能修改');
            }
            if(!empty($form['password'])){
                $form['salt'] = getSalt(6);
                $form['password'] = md5(md5($form['password']).$form['salt']);
            }
            UserModel::instance()->where(['id'=>$id])->update($form);
            $this->success('成功','');
        }else{
            $this->assign('model',$model);
            return $this->fetch('user');
        }
    }

    public function remove(){
        $id = Request::param('id/d');
        if(in_array($id,Config::get('web.superuser'))){
            $this->error('不能删除系统用户');
        }
        UserModel::instance()->where(['id'=>$id])->delete();
        $this->success('操作成功');
    }

    public function setauth(){
        $id = Request::param('id/d');
        $model = UserModel::instance()->where(['id'=>$id])->findOrEmpty();
        if(!$model){
            $this->error('参数错误','');
        }
        if(Request::isPost()){
            $res = null;
            $type = Request::param('type/s');
            $authlist = AuthModel::instance()->getAllAuths();
            switch ($type)
            {
                case 'getauthlist':
                    $data = AuthModel::instance()->getNewAuthlist($authlist);
                    $authed = AuthModel::instance()->getUserAuthList($id);
                    $authed = array_keys($authed);
                    $res = json(['code'=>0,'msg'=>'数据列表','data'=>$data,'authed'=>$authed]);
                    break;
                default:
                    $auths = Request::param('auth/a');
                    AuthModel::instance()->setUserAuth($id,$auths);
                    $res = json(['code'=>0,'msg'=>'成功']);
                    break;
            }
            return $res;
        }else{
            $this->assign('id',$id);
            return $this->fetch('setauth');
        }
    }

    public function setgroup(){
        $id = Request::param('id/d');
        $model = UserModel::instance()->where(['id'=>$id])->findOrEmpty();
        if(!$model){
            $this->error('参数错误','');
        }
        if(Request::isPost()){
            $gid = Request::param('gid/a');
            UserModel::instance()->setUserGroups($id,$gid);
            $this->success('成功','',$gid);
        }
        else if(Request::isAjax()){
            $usergroups = UserModel::instance()->getUserGroups($id);
            $groups = array();
            foreach($usergroups as $v){
                $groups[] = $v['gid'];
            }
            $list = GroupModel::instance()->select();
            foreach($list as $k=>$v){
                if(in_array($v['guid'],$groups))
                    $v['LAY_CHECKED'] = true;
                $list[$k] = $v;
            }
            return json(['code'=>0,'msg'=>'成功','data'=>$list]);
        }
        else{
            return $this->fetch();
        }

    }

}